// pages/communication/communication.js
import {baseURL, request} from"../../utils/request"
const comm = require("../../api/comm")

Page({
  /**
   * 页面的初始数据
   */
  data: {
    //页面的数据
    comments: [
      {
        avatar: "",
        username: "",
        datetime: "",
        content: "",
        imageUrls: "",
      }
    ],
    inputValue: '',
    imageUrl: "",
    baseURL: baseURL,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.communicationList();
  },

  //查询所有的评论信息
  communicationList(){
    comm.getData().then(res=>{
      if(res.code == 200){
        console.log("Data:",res)
        this.onInput(res.data)
      }
      console.log("commData:",res.data)
    })
  },
  //赋值
  onInput(data){
    let comments = [];
    for (let i = 0; i < data.length; i++) {
      comments.push({
        avatar: wx.getStorageSync("userInfo").imageUrl,
        username: wx.getStorageSync("userInfo").username,
        datetime: data[i].time,
        content: data[i].content,
        imageUrls: data[i].image
      })
    }
    this.setData({
      comments: comments
    })
  },
  //输入框内容
  handleInput(event) {
    this.setData({
      inputValue: event.detail.value
    });
  },

  //图片上传
  chooseImage() {
    const that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(result) {
        console.log("1",result.tempFilePaths)
        that.setData({
          imageUrl: result.tempFilePaths[0]
        });
      }
    });
  },

 //保存评论信息
  handleSubmit(event) {
    if (!this.data.inputValue && !this.data.imageUrl) {
      wx.showToast({
        title: '请输入评论或上传图片',
        icon: 'none'
      });
      return;
    }

    var commvo = {
      communicationId: "",
      userId: wx.getStorageSync("userInfo").userId,
      content: this.data.inputValue,
      time: "",
      image: this.data.imageUrl
    }
    console.log("baocun:",commvo);
    comm.savaData(commvo).then(res=>{
      if(res.code == 200){
        console.log("结果：",res)
        this.communicationList();
        this.setData({
          inputValue: '',
          imageUrl: '',
        });
      }
      
    })

    wx.showToast({
      title: '评论成功',
      icon: 'success'
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})